package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.Food;
import com.ruoyi.system.domain.vo.FoodVo;
import com.ruoyi.system.domain.vo.ShopDataVo;
import com.ruoyi.system.domain.vo.ShopVo;
import com.ruoyi.system.mapper.FoodMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ShopMapper;
import com.ruoyi.system.domain.Shop;
import com.ruoyi.system.service.IShopService;

/**
 * 商铺信息Service业务层处理
 *
 * @author Wxl
 * @date 2022-03-13
 */
@Service
public class ShopServiceImpl implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SysDictDataMapper dictMapper;
    @Autowired
    private FoodMapper foodMapper;

    /**
     * 查询商铺信息
     *
     * @param id 商铺信息主键
     * @return 商铺信息
     */
    @Override
    public Shop selectShopById(Long id) {
        return shopMapper.selectShopById(id);
    }

    /**
     * 查询商铺信息列表
     *
     * @param shop 商铺信息
     * @return 商铺信息
     */
    @Override
    public List<Shop> selectShopList(Shop shop) {
        return shopMapper.selectShopList(shop);
    }

    /**
     * 新增商铺信息
     *
     * @param shop 商铺信息
     * @return 结果
     */
    @Override
    public int insertShop(Shop shop) {

        long id = new Date().getTime();
        shop.setId(id);
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictLabel(shop.getName());
        sysDictData.setDictValue(String.valueOf(id));
        sysDictData.setDictSort(999L);
        sysDictData.setDictType("shop_id");
        sysDictData.setCssClass("1");
        sysDictData.setListClass("primary");
        sysDictData.setStatus("0");
        sysDictData.setIsDefault("N");
        sysDictData.setCreateBy("admin");
        dictMapper.insertDictData(sysDictData);
        return shopMapper.insertShop(shop);
    }

    /**
     * 修改商铺信息
     *
     * @param shop 商铺信息
     * @return 结果
     */
    @Override
    public int updateShop(Shop shop) {
        return shopMapper.updateShop(shop);
    }

    /**
     * 批量删除商铺信息
     *
     * @param ids 需要删除的商铺信息主键
     * @return 结果
     */
    @Override
    public int deleteShopByIds(Long[] ids) {
        return shopMapper.deleteShopByIds(ids);
    }

    /**
     * 删除商铺信息信息
     *
     * @param id 商铺信息主键
     * @return 结果
     */
    @Override
    public int deleteShopById(Long id) {
        return shopMapper.deleteShopById(id);
    }

    @Override
    public List<ShopVo> getShopByCanteenId(Long canteenId) {
        List<ShopVo> shopVoList = new ArrayList<>();
        List<Shop> shopList = shopMapper.selectList(new QueryWrapper<Shop>().eq("canteen_id", canteenId));
        for (Shop shop : shopList) {
            ShopVo shopVo = new ShopVo();
            shopVo.setId(shop.getId());
            shopVo.setName(shop.getName());
            String Avatar = "http://localhost:8000/dev-api"+shop.getLogoImg();
            shopVo.setAvatar(Avatar);
            shopVo.setDistance("300m");
            shopVo.setDistance(shop.getSendTime());
            shopVo.setFloorSendCost(shop.getFloorSendCost());
            shopVo.setMark(shop.getScore());
            shopVo.setSendCost(shop.getSendCost());
            shopVo.setMonthSales(shop.getMonthSales());
            List<FoodVo> foodVoList = new ArrayList<>();
            List<Food> foods = foodMapper.selectList(new QueryWrapper<Food>());
            for (Food food : foods) {
                FoodVo foodVo = new FoodVo();
                foodVo.setId(food.getId());
                foodVo.setName(food.getFoodName());
                foodVo.setImg_url("http://localhost:8000/dev-api"+food.getFoodCover());
                foodVo.setOld_price(food.getOriginPrice());
                foodVo.setTag(food.getTag());
                foodVo.setPrice(food.getSellPrice());
                foodVoList.add(foodVo);
            }
            shopVo.setGoods(foodVoList);
            shopVoList.add(shopVo);
        }
        return shopVoList;
    }

    @Override
    public ShopDataVo getShopDataVo(Long ShopId) {
        Shop shop = shopMapper.selectOne(new QueryWrapper<Shop>().eq("id", ShopId));
        ShopDataVo shopDataVo = new ShopDataVo(
                shop.getId(),
                shop.getName(),
                "http://localhost:8000/dev-api"+shop.getLogoImg(),
                "http://localhost:8000/dev-api"+shop.getInstoreImg(),
                shop.getNotice(),
                "桃李园食堂", "3楼",
                shop.getSendTime(),
                shop.getContactMobile(),
                new ArrayList(),
                shop.getSendCost().toString(),
                shop.getFloorSendCost().toString(),
                shop.getBoxCost().toString());
        return shopDataVo;
    }
}
